import BaseSprite from "../../../Script/Base/BaseSprite";
import { EResType } from "../../../Script/Common/Enum";
import { LoadUtil } from "../../../Script/Util/LoadUtil";

export enum EDragType {
    Default,
    Drag,
    Click,
}

export interface IDragData {
    id: number;
    index: number;
    type: EDragType;
    name: string;
    callback?: Function;
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemDrag extends BaseSprite {

    @property(cc.Sprite)
    private icon: cc.Sprite = null;
    @property(cc.Label)
    private labName: cc.Label = null;

    private data: IDragData = null;

    protected onLoad() {
        this.node.y = 0;
    }

    public setData(data: IDragData) {
        this.data = data;
        this.labName.string = `${data.name}`;
        LoadUtil.instance.setSpriteFrame(this.icon, EResType.StoryIcon, `story_icon_${data.id}`);

        if (this.data.type === EDragType.Click) {
            this.icon.node.active = false;
        }
    }

    public showIcon(index: number) {
        if (this.data.type === EDragType.Click) {
            if (this.data?.index === index) {
                this.icon.node.active = true;
            }
        }
    }

    public getId() {
        return this.data?.id;
    }
}